<template>
  <div class="content">
    <div class="header">
      <div style="width:20px;height:10px;"></div>
      <el-button type="primary">下载数据模板</el-button>
    </div>
    <div class="listseach">
      <div class="itemthree">
        <div class="sechname">数据日期</div>
        <el-date-picker
          v-model="value1"
          type="daterange"
          style="width:300px;"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <div class="itemthree">
        <div class="sechname">主题</div>
        <el-input style="width:300px;" v-model="them"></el-input>
      </div>
      <div class="itemthree">
        <div class="sechname">选择数据源</div>
        <el-upload
          class="upload-demo"
          :action="$src+'/api/upload'"
          multiple
          name="file"
          :limit="1"
          :on-error="error"
          :file-list="file_list"
          :on-success="success"
            accept=".xls, .xlsx"

        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </div>
      <div class="itemthree">
        <el-button type="primary" @click.native="upload">上传</el-button>
      </div>
    </div>
    <div class="datalist">
      <el-table  :cell-style="$rowstyle"
        :header-cell-style="$headerstyle" :data="tableData" border style="width: 100%">
        <el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
        <el-table-column label="数据日期" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.start}}~{{scope.row.end}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="them" align="center" label="主题"></el-table-column>
        <el-table-column prop="get_admin_info.name" label="上传人" align="center"></el-table-column>
        <el-table-column prop="created_at" align="center" label="上传时间"></el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="delitem(scope.row)" type="text" size="small">删除</el-button>
            <!-- <el-button type="text" size="small">重新上传</el-button> -->
            <el-upload
              style="display:inline-block"
              class="upload-demo"
              :action="$src+'/api/upload'"
              multiple
              name="file"
              :limit="1"
              :show-file-list="false"
              :on-error="error"
          :file-list="fileList"
              :on-success="editsuccess"
            >
              <el-button size="small" type="text" @click.native="editupload(scope.row)">重新上传</el-button>
            </el-upload>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="float:right;margin-top:10px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="page"
          :page-size="10"
          layout="prev, pager, next, jumper"
          :total="alltotal"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { getDataList, addDataList, editData } from "@/api/Salesman.js";
import { SETDATE } from "@/api/System.js";
export default {
  data() {
    return {
      page: 1,
      value1: [],
      alltotal: 0,
      tableData: [],
     file_list:[],
     fileList:[],
      them: "",
      file_path: "",
      edit: {
        type: "",
        id: "",
        date_range: "",
        them: "",
        file_path: ""
      }
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    //修改
    editD(data) {
      editData(data).then(res => {
        if (data.type == 2) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
          this.fileList=[]
        }
        if (data.type == 1) {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        }
        this.page = 1;
        this.getlist();
      });
    },
    //从新上传
    editsuccess(e) {
      if (e.code == 200) {
        this.edit.file_path = e.data.path;
        this.editD(this.edit);
      }
    },

    editupload(row) {
      this.edit.date_range = JSON.stringify(row.date_range);
      this.edit.id = row.id;
      this.edit.them = row.them;
      this.edit.type = 2;
    },
    //点击上传
    upload() {
      if (this.value1.length < 1) {
        this.$message({
          message: "数据日期不能为空",
          type: "warning"
        });
        return;
      }
      if (!this.them) {
        this.$message({
          message: "主题不能为空",
          type: "warning"
        });
        return;
      }
      if (!this.file_path) {
        this.$message({
          message: "数据源不能为空",
          type: "warning"
        });
        return;
      }
      var date_range = {
        start_time: new Date(this.value1[0]).getTime() / 1000,
        end_time: new Date(this.value1[1]).getTime() / 1000
      };
      var data = {
        date_range: JSON.stringify(date_range),
        them: this.them,
        file_path: this.file_path
      };
      addDataList(data).then(res => {
        if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
          this.page = 1;
          this.value1 = [];
          this.file_path = "";
          this.them = "";
          this.file_list=[]

          this.getlist();
        }
      });
    },
    //文件上传
    success(e) {
      if (e.code == 200) {
        this.file_path = e.data.path;
      }
    },
    error() {
      this.$message({
        message: "上传失败"
      });
    },
    //获取列表
    getlist() {
      let data = {
        page: this.page,
        per: 10
      };
      getDataList(data).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.data;
          this.page = res.data.current_page;
          this.alltotal = res.data.total;
          this.tableData.forEach(element => {
            element.created_at = SETDATE(element.created_at);
            if (element.date_range) {
              var date_range = JSON.parse(element.date_range);
              element.date_range = date_range;
              element.start = SETDATE(element.date_range.start_time, true);
              element.end = SETDATE(element.date_range.end_time, true);
            }
          });
        }
      });
    },
    //删除数据
    delitem(row) {
      var _this = this;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        distinguishCancelAndClose: true,
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var data = {
            type: 1,
            id: row.id
          };
          editData(data).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.page = 1;
            this.getlist();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange() {},
    handleCurrentChange(e) {
      this.page = e;

      this.getlist();
    }
  }
};
</script>
<style lang="scss" scoped>
.header {
  height: 40px;
  width: 97%;
  display: flex;
  margin: 0 auto;
  align-items: center;
  flex-direction: row-reverse;
}
.Search {
  display: flex;
  align-items: center;
  margin-left: 30px;
}
.listseach {
  width: 97%;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  .itemthree {
    width: 50%;
    display: flex;
    align-items: center;
    margin-top: 20px;
    .sechname {
      width: 90px;
      font-size: 14px;
    }
  }
}
.datalist {
  width: 97%;
  margin: 0 auto;
  margin-top: 20px;
}
</style>